package handler

import (
	"evaluation/api/request"
	"evaluation/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddComment(c *gin.Context) {
	var req request.AddCommentForm
	var comment model.Comment
	var banned model.Banned

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	ui := c.GetUint("token")

	banned.Banned = req.Content
	err := banned.GetBanned()
	if err != nil {
		return
	}

	if banned.Id != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "包含敏感词",
			"data":    "",
		})
		return
	}

	comment = model.Comment{
		UserId:  int32(ui),
		VideoId: req.VideoId,
		Content: req.Content,
	}

	err = comment.AddComment()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "评论失败",
			"data":    "",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "评论成功",
		"data":    comment.Content,
	})
}
